package cn.example.service;

import cn.example.mapper.SysRoleMapper;
import cn.example.pojo.SysRole;
import cn.example.utils.RedisUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService{

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public List<SysRole> getSysRole(){
        if(!redisUtil.exist("roleList")){
            List<SysRole> sysRoleList = sysRoleMapper.getSysRole();
            String s = JSON.toJSONString(sysRoleList);
            redisUtil.set("roleList",s);
            return sysRoleList;
        }else{
            String roleList = redisUtil.get("roleList").toString();
            List<SysRole> sysRoleLsit = JSON.parseArray(roleList,SysRole.class);
            return sysRoleLsit;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addSysRole(SysRole sysRole) {

        int i = sysRoleMapper.addSysRole(sysRole);
        return i;
    }
}
